var xmlHttp = false;
try{xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){
	try{xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
	catch(e2){xmlHttp=false;}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){xmlHttp = new XMLHttpRequest();}

String.prototype.trim=function () {
    return this.replace(/(\s*$)|(^\s*)/g,'');
}

function $(id) {
    return document.getElementById(id);
}

function $F(id) {
    var o=$(id);
    if (o==null) {
        return null;
    }else {
        return o.value.trim();
    }
}

function Ajax(url)
{
	var m_xmlReq=null;
	if(window.ActiveXObject)
	{
	    try
	    {
	        m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP');
	    }
	    catch(e)
	    {
	        try{m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
	    }
	}
	else if(window.XMLHttpRequest)
	{
	    m_xmlReq = new XMLHttpRequest();
	}

	this.post=function(d)
	{
	    if(!m_xmlReq)  return;
	    m_xmlReq.open('POST',url,false);
	    m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=gb2312');
	    m_xmlReq.send(d);
	    return eval(m_xmlReq.responseText);
	}
}


function checkUserLoginIsVal() {

    var news_user_login_state_ajax=new Ajax('http://'+location.hostname+'/ajax/news_user_login_state.php');

    var demo=news_user_login_state_ajax.post();

    if (!demo) {

        alert('请先登录!');

        $('comments_iframe').style.display='block';

        $('login_name').focus();

        return false;

    }
    return true;

}

function checkCodeIsVal() {

    var vcode=$F('checkcode');

    if (vcode=='') {

        alert('请输入验证码!');

        $('checkcode').value='';

        $('checkcode').focus();

        return false;
    }
    if (vcode.length!=4) {

        alert('验证码的长度不正确!');

        $('checkcode').value='';

        $('checkcode').focus();

        return false;
    }

    var vcode_ajax=new Ajax('http://'+location.hostname+'/ajax/check_code.php');

    if (!vcode_ajax.post('vcode='+escape(vcode))) {

        alert('验证码输入错误!');

        $('checkcode').value='';

        $('checkcode').focus();

        return false;
    }
    return true;
}

function checkNewsCommentsContent() {

    var cont=$F('comment');

    if (cont=='') {

        alert('请输入评论内容!');

        $('comment').focus();

        return false;
    }
    if (cont.length<10 || cont.length>200) {

        alert('评论内容必须在10~200字符之间!');

        $('comment').focus();

        return false;
    }
    return true;
}

function addComments() {

    if (checkUserLoginIsVal() && checkNewsCommentsContent() && checkCodeIsVal()) {

        var news_cont=$F('comment');

        var add_comments_ajax=new Ajax('http://'+location.hostname+'/ajax/news_comments.php');

        var str=add_comments_ajax.post('news_id='+escape(news_id)+'&news_cont='+encodeURIComponent(news_cont));

        if (str) {

            alert('评论发表成功!');

            showCommentsList();

            // alert(strings);

           // $('news_comments').innerHTML=strings;

            return false;

        }else {

            alert('发表评论失败,请联系管理员!');

            return false;

        }

    }else {

        return false;
    }

}

function showCommentsList() {

    var urls='http://'+location.hostname+'/n_tugu/ajax/news_comments_list_show.php';

    var show_comments_list_ajax=new Ajax('http://'+location.hostname+'/ajax/news_comments_list_show.php');

    $('news_comments').innerHTML=show_comments_list_ajax.post('nid='+escape(news_id));

}

function news_user_login() {

    var username=$F('login_name');

    var pwd=$F('login_pwd');

    if (username=='') {

        alert('请输入用户名!');

        $('login_name').focus();

        return false;
    }
    if (pwd=='') {

        alert('请输入密码!');

        $('login_pwd').focus();

        return false;
    }

    var user_login_ajax=new Ajax('http://'+location.hostname+'/ajax/news_user_login.php');

    if (user_login_ajax.post('username='+username+'&pwd='+pwd)) {

        document.getElementById('comments_iframe').style.display='none';

        return true;
    }else{

        alert('用户名或密码错误!');

        $('login_pwd').focus();

        return false;
    }

}

function add_comments() {

        var news_cont=$F('comment');

        var add_comments_ajax=new Ajax('http://'+location.hostname+'/ajax/news_comments.php');

        if (add_comments_ajax.post('news_id='+escape(news_id)+'&news_cont='+news_cont)) {

            alert('评论发表成功!');

            return false;

        }else {

            alert('发表评论失败,请联系管理员!');

            return false;

        }

}